package com.sojson.project.sys.log.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.project.sys.log.entity.dto.LogLoginDto;
import com.sojson.project.sys.log.entity.dto.LogLoginPageDto;
import com.sojson.project.sys.log.entity.po.LogLogin;
import com.sojson.project.sys.log.entity.vo.LogLoginVo;
import com.sojson.project.sys.log.service.LogLoginService;
import com.sojson.result.Result;
import com.sojson.util.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 系统访问记录控制器
 * 
 * @author liu
 * @date 2022-10-24
 */
@Api(tags = "系统访问记录管理")
@RestController
@RequestMapping("/sys/log/login")
public class LogLoginController {

    @Autowired
    private LogLoginService logLoginService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/save')")
    @ApiOperation("给系统访问记录添加一条数据")
    public Result<Boolean> save(@RequestBody LogLogin dto) {
        return Result.success(logLoginService.save(dto));
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/updateById')")
    @ApiOperation("通过id更新系统访问记录数据")
    public Result<Boolean> updateById(@RequestBody LogLogin dto) {
        return Result.success(logLoginService.updateById(dto));
    }

    /**
     * 列表查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/list')")
    @ApiOperation(value = "查询系统访问记录数据", response = LogLoginVo.class)
    public Result<List<LogLoginVo>> list(LogLoginDto dto) {
        return Result.success(logLoginService.list(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/page')")
    @ApiOperation(value = "分页查询系统访问记录数据", response = LogLoginVo.class)
    public Result<List<LogLoginVo>> page(LogLoginPageDto dto) {
        return Result.success(logLoginService.page(dto));
    }

    /**
     * 导出数据
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/export')")
    @ApiOperation("导出系统访问记录表数据")
    public void export(LogLoginDto dto) throws Exception {
        List<LogLoginVo> list = logLoginService.list(dto);
        ExcelUtil<LogLoginVo> excelUtil = new ExcelUtil<>(LogLoginVo.class);
        excelUtil.exportExcelOutFile(list, "系统访问记录");
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/info/{id}')")
    @ApiOperation(value = "通过id查询系统访问记录数据", response = LogLoginVo.class)
    public Result<LogLoginVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(logLoginService.findById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/remove')")
    @ApiOperation("通过id列表删除系统访问记录数据")
    public Result<Boolean> remove(@RequestBody LogLoginDto dto) {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(logLoginService.removeByIds(ids));
    }

    /**
     * 清空数据
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/clear')")
    @ApiOperation("清空系统访问记录数据")
    public Result<Boolean> clear() {
        return Result.success(logLoginService.clear());
    }

    /**
     * 通过id删除
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("@ss.hasPermi('/sys/log/login/remove/{id}')")
    @ApiOperation("通过id删除系统访问记录数据")
    public Result<Boolean> remove(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(logLoginService.removeById(id));
    }

}